USE [UFDATA_001_2024]
GO

/****** Object:  Table [dbo].[xj_inv]    Script Date: 2024/8/27 3:16:43 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[xj_inv](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [cinvcode] [varchar](100) NULL,
    [operate] [varchar](50) NULL,
    [category] [varchar](50) NULL,
    [createtime] [datetime] NULL
    ) ON [PRIMARY]
    GO





create      TRIGGER   [dbo].[xj_getCurrentstock]  ON [dbo].[CurrentStock]
for   UPDATE ,insert
      NOT FOR REPLICATION
      AS
begin
declare  @autoid INT
DECLARE  cur    CURSOR  FOR select autoid  from inserted
    OPEN   cur
FETCH next  FROM  cur  INTO   @autoid
 while @@fetch_status=0
BEGIN
DECLARE @invcode  VARCHAR(100)
select @invcode =c.cinvcode  FROM  currentstock  c
  INNER JOIN  Inventory  Inv ON c.cInvCode=inv.cInvCode
WHERE  Inv.cInvCCode >='02003001'  AND  Inv.cInvCCode<='02003007'
  and  c.cwhcode='bzp'  and  c.autoid=@autoid
 if  isnull(@invcode,'')!=''
begin
insert into xj_Inv(operate,category,cinvcode,createtime) values('insert','stock',@invcode,getdate())
end
fetch next from cur into @autoid
end
close cur
 deallocate cur
end



CREATE       TRIGGER   [dbo].[xj_getinventory ]  ON  inventory
FOR   insert
NOT FOR REPLICATION
AS
begin
DECLARE @invcode  VARCHAR(100)
select @invcode=cinvcode  from inserted
if  isnull(@invcode,'')!=''
begin
insert into xj_Inv(operate,category,cinvcode,createtime) values('insert','inventory',@invcode,getdate())
end
end


CREATE         TRIGGER   [dbo].[xj_pricejust ]  ON  SA_InvPriceJustMain
FOR   insert,update   NOT FOR REPLICATION
   AS
    set nocount on
    if   update(cverifier)
begin
   DECLARE @vouchcode  VARCHAR(100)
select @vouchcode=ccode  from ((select  distinct m.ccode as ccode from  SA_InvPriceJustMain m
   inner join inserted  on m.id=inserted.id
   inner join SA_InvPriceJustDetail detail
   inner join  inventory  inv  on  inv.cInvCode=detail.cinvcode
    on m.id=detail.id  where inv.cInvCCode >='02003001' and  cInvCCode<='02003007'))v

   if  isnull(@vouchcode,'')!=''
begin
insert into xj_Inv(operate,category,cinvcode,createtime) values('insert','pricejust',@vouchcode,getdate())
end
end
set  nocount off

CREATE   TRIGGER [dbo].[xj_transContact] ON [dbo].[DispatchList]
FOR INSERT   AS
set nocount on
UPDATE  a SET     ccontactname=   a.cdefine12  FROM  DispatchList a
INNER  JOIN  inserted  ON   a.dlid=inserted.dlid
    set nocount off


create       TRIGGER   [dbo].[xj_updateCurrentstock]  ON [dbo].[CurrentStock]
FOR  delete
NOT FOR REPLICATION
AS
begin
   DECLARE @invcode  VARCHAR(100)
   declare cur  cursor  for  select cinvcode  from  deleted  where cwhcode='bzp'
    open cur
    fetch next  from cur  into @invcode
      while @@fetch_status=0
begin
select @invcode =inv.cinvcode  FROM Inventory  inv
WHERE  Inv.cInvCCode >='02003001'  AND  Inv.cInvCCode<='02003007' and inv.cinvcode=@invcode
    if  isnull(@invcode,'')!=''
begin
insert into xj_Inv(operate,category,cinvcode,createtime) values('delete','stock',@invcode,getdate())
end
fetch next  from cur  into @invcode
end
close cur
deallocate cur
end


